<?php
/**
 * @Copyright   2008, XXXX-XXXX
 * All rights reserved.
 * feature summary.
 * @filesource:	request_item.php 
 * @author      	lipeng <lipengage@gmain.com> 
 * @version     	$Id: $ 
 * @package     	application
 * @subpackage   service
 */
/**
 * Represents the request items that come from the http request.
 */
class request_item {
	protected static $X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
	protected $params = false;
	protected $uri = false;
	protected $domain = false;
	protected $token = false;
	protected $format = false;
	protected $method = false;
	protected $postdata = false;

	/**
	 * get data from the uri and the http parameters
	 * slice this data to 
	 * set this class attributes
	 */
	public function __construct() {
		$this->params = self::create_parameter_map();
		$this->uri = $_SERVER['REQUEST_URI'];
		$this->domain = $this->params['domain'];
		$this->token = $this->params['token'];
		if (isset($this->params['format'])) {
			$this->format = $this->params['format'];
		}
		else {
			if (isset($_SERVER['CONTENT_TYPE'])) {
				switch ($_SERVER['CONTENT_TYPE']) {
					case 'application/atom+xml':
						$this->format = 'atom';
						break;
					case 'application/xml':
						$this->format = 'xml';
						break;
					case 'application/json':
						$this->format = 'json';
						break;
				}
			}
		}
		if (isset($this->params['method'])) {
			$this->method = $this->params['method'];
		}
		else {
			$this->method = $_SERVER['REQUEST_METHOD'];
			if ($this->method == 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
				$this->method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
			}
		}
		if (isset($this->params['postdata'])) {
			$this->postdata = $this->params['postdata'];
		}
	}

	/**
	 * set $this->params,
	 *
	 * @param $params a mix data
	 */
	public function set_params($params) {
		$this->params = $params;
	}

	/**
	 * set $this->uri,
	 *
	 * @param $uri a string
	 */
	public function set_uri($uri) {
		$this->url = $uri;
	}

	/**
	 * set $this->domain, the domain be used, or not
	 *
	 * @param $domain a string
	 */
	public function set_domain($domain) {
		$this->domain = $domain;
	}
	
	/**
	 * set $this->token, the token be used, or not
	 *
	 * @param $token a string
	 */
	public function set_token($token) {
		$this->token = $token;
	}

	/**
	 * set $this->method
	 *
	 * @param $method a string
	 */
	public function set_method($method) {
		$this->params['method'] = $method;
		$this->method = $method;
	}
	
	/**
	 * set $this->postdata
	 *
	 * @param $postdata mix data
	 */
	public function set_postdata($postdata) {
		$this->params['postdata'] = $postdata;
		$this->postdata = $postdata;
	}
	
	/**
	 * set $this->params[]
	 *
	 * @param $param_name a key
	 * @param $param_value a value
	 */
	public function set_parameter($param_name, $param_value) {
		if ($param_value == null) {
			return;
		}
		$this->params[$param_name] = $param_value;
	}
	
	 /**
	 * @return a string
	 */
	public function get_params() {
		return $this->params;
	}
	
	/**
	 * @return a string
	 */
	public function get_uri() {
		return $this->uri;
	}
	
	/**
	 * @return a string
	 */
	public function get_domain() {
		return 'owner';
		return $this->domain;
	}
	
	/**
	 * @return a string
	 */
	public function get_token() {
		return true;
		//return $this->token;
	}
	
	/**
	 * @return a string 
	 */
	public function get_format() {
		return $this->format;
	}
	
	/**
	 * @return a string
	 */
	public function get_method() {
		return $this->method;
	}

	/**
	 * @return a string postdata
	 */
	public function get_postdata() {
		return $this->postdata;
	}

	/**
	 * @return a param one params['']
	 */
	public function get_parameter($param_name) {
		return $this->params[$param_name];
	}

	/**
	 * @return a list param value
	 */
	public function get_list_parameter($param_name) {
		$string_list = isset($this->params[$param_name]) ? $this->params[$param_name] : null;
		if ($string_list == null) {
			return array();
		} elseif (is_array($string_list)) {
			// already converted to array, return straight away
			return $string_list;
		}
		if (strpos($string_list, ',') !== false) {
			$string_list = explode(',', $string_list);
		} else {
			// Allow up-conversion of non-array to array params.
			$string_list = array($string_list);
		}
		$this->params[$param_name] = $string_list;
		return $string_list;
	}

	/**
	 * @return an object request_item
	 */
	public static function create_with_request() {
		$request_item = new request_item();
		return $request_item;
	}

	/**
	 * @return a string request_method
	 */
	public static function get_http_method() {
		if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
			return $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
		} else {
			return $_SERVER['REQUEST_METHOD'];
		}
	}

	/**
	 * @return an array merge addslashes $_POST,$_GET
	 */
	public static function create_parameter_map() {
		$params = array_merge($_POST, $_GET);
		request_item::addslashes_($params);
		return $params;
	}

	/**
	 * escape the parameter 
	 *
	 * @param $v a string need slashes 
	 * @return null
	 */
	public static function addslashes_($v) {
		if (!get_magic_quotes_gpc()) {
			request_item::addslashes_deep($v);
		}
		return ;
	}

	/**
	 * escape the parameter
	 *
	 * @param $v a string need slashes 
	 * @return null
	 */
	public static function addslashes_deep(&$v) {
		if (is_array($v)) {
			foreach ($v as $_k=>$_v) {
				request_item::addslashes_deep($v[$_k]);
			}
		}
		else {
			$v = addslashes($v);
		}
	}
	
	/**
	 *
	 *
	 */
}